package com.lp.module_lib.base;

import android.os.Bundle;

import com.alibaba.android.arouter.launcher.ARouter;
import com.lp.module_lib.R;
import com.lp.module_lib.api.BasePresenter;
import com.lp.module_lib.network.mvp.BaseModel;
import com.lp.module_lib.network.mvp.BaseObserver;
import com.lp.module_lib.network.mvp.BaseView;
import com.lp.module_lib.utils.AppManager;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 作者：created by LP_TREAM on 2020/11/26 19:41
 * 邮箱：806875201@qq.com
 */
public abstract class BaseActivity<P extends BasePresenter> extends SupportActivity implements BaseView {
    protected P mPresenter;

    protected abstract P createPresenter();

    private Unbinder mBind;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        mActivity = this;

        mPresenter = createPresenter();

        setContentView(getLayoutId());

        AppManager.getAppManager().addActivity(this);

        ARouter.getInstance().inject(this);
        mBind=ButterKnife.bind(this);

        setStatusBar();

        initData();

        initRefreshLayout(setRefreshLayout());
    }

    /**
     * 获取布局ID
     *
     * @return
     */
    protected abstract int getLayoutId();

    /**
     * 数据初始化操作
     */
    protected abstract void initData();

    @Override
    public void onErrorState(BaseModel model, int mType) {
        showToast(model.getMsg());
        if (mSmoothRefreshLayout != null) mSmoothRefreshLayout.refreshComplete(false);
    }

/*
    @Override
    public void showLoading() {
        showLoadingDialog();
    }
*/

    @Override
    public void showLoading(Boolean isShowProgress) {
        if (isShowProgress){
            showProgressDialog();
        }else {
            showLoadingDialog();
        }
    }

    @Override
    public void hideLoading() {
        dissMissDialog();
        hideProgressDialog();
    }

    public void showLoadingDialog() {
        showLoadingDialog(getString(R.string.app_loading));
    }

    @Override
    public void onProgress(int progress) {
        onProgressDialog(progress);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mBind.unbind();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }
}
